package com.smart.manager.task;


import cn.hutool.core.util.IdUtil;
import com.smart.common.annotation.TaskBean;
import com.smart.common.annotation.TaskMethod;
import com.smart.common.entities.domain.TaskResult;
import com.smart.manager.member.entities.enums.ChickenState;
import com.smart.manager.member.entities.models.MemberMaterialChicken;
import com.smart.manager.member.entities.models.query.QMemberProfileMain;
import com.smart.manager.member.entities.views.MemberView;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

@Log4j2
@Component
@TaskBean
@RequiredArgsConstructor
public class ChickenOrdinaryTask {

    @TaskMethod(title = "鸡蛋生成任务(普通用户)")
    public synchronized TaskResult chickenTask() {
        LocalDate date = LocalDate.now();
        int intermediate = 60; // 设定生成概率为60%
        try {
            List<Long> memberId = new QMemberProfileMain().select(QMemberProfileMain.Alias.id).findSingleAttributeList();
            String sql = "SELECT DISTINCT member FROM member_material_pig AS pig WHERE pig.chicken IS NOT NULL AND pig.chicken != 0 AND pig.deleted = false AND pig.chicken >= 5";
            Random random = new Random();
            DB.sqlQuery(sql).findEach(x -> {
                Long member = x.getLong("member");
                memberId.remove(member);
            });
            HashSet<Long> longs = new HashSet<>(memberId);
            longs.forEach(x -> {
                //每个人循环3次
                int winning = 0;
                String uuid = IdUtil.fastSimpleUUID().substring(0, 8).toUpperCase();
                List<MemberMaterialChicken> chickens = new ArrayList<>();
                for (int j = 0; j < 3; j++) {
                    int randomNumber = random.nextInt(100); // 生成0到99之间的随机数
                    if (randomNumber < intermediate) {
                        winning++;
                    }
                }
                //保底
                if (winning < 1) {
                    winning = 1;
                }
                for (int i = 0; i < winning; i++) {
                    int i1 = random.nextInt(2);
                    int withHour = 0;
                    if (i1 == 1) {
                        withHour = random.nextInt(5) + 7;  //生成上午的小时数
                    } else {
                        withHour = random.nextInt(18 - 12 + 1) + 12;  //生成下午的小时
                    }
                    int withMinute = random.nextInt(60);  //生成上午的小时数
                    int withSecond = random.nextInt(60);  //生成上午的小时数
                    LocalDateTime localDateTime = LocalDateTime.now().withHour(withHour).withMinute(withMinute).withSecond(withSecond);
                    MemberMaterialChicken chicken = new MemberMaterialChicken()
                            .setCode(uuid).setPigCode(null)
                            .setGenerationTime(date)
                            .setDisplayTime(localDateTime)
                            .setMember(new MemberView().setId(x))
                            .setState(ChickenState.L1);
                    chickens.add(chicken);
                }
                //生成
                DB.insertAll(chickens);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new TaskResult(true, "生成成功");
    }
}
